﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.TA
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        //string enteredID;
        static string enteredName;
        string id;

        string strProvider = Resource1.databaseConnection;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[3].Selected = true;

            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            id = (string)Session["userName"];
            if (id == null)
                Response.Redirect("../login.aspx");


            string getSelectedInfo = "SELECT *"
                                 + "FROM gradstudent "
                                 + "WHERE sid = " + id;

            sqlCommand = new MySqlCommand(getSelectedInfo, conn);
            readVals = sqlCommand.ExecuteReader();

            readVals.Read();
            Name.Text = "" + readVals.GetString("s_name");
            enteredName = Name.Text;
  
            Email.Text = "" + readVals.GetString("s_email");

            ImgUrl.Text = "" + readVals.GetString("s_url");
            Password.Text = "" + readVals.GetString("s_pass");
            conn.Close();
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            //Get info from textboxes
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string name = Name.Text;
            string email = Email.Text;
            string pass = Password.Text;
            string url = ImgUrl.Text;

            string comma = ", ";

            string modifyRa = "UPDATE gradstudent "
                                 + " SET s_name=" + "'" + name + "'" + comma + "s_email=" + "'" + email + "'" + comma + "s_pass=" + "'" + pass + "'" + comma + "s_url=" + "'" + url + "'"
                                  + " WHERE sid=" + id;
            sqlCommand = new MySqlCommand(modifyRa, conn);
            sqlCommand.ExecuteNonQuery();

            conn.Close();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("TA_Welcome.aspx");
        }
    }
}